﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Cryptography;
using System.Text;
using BudgetSV.Common;

namespace BudgetSV.admin
{
    public partial class login : ManagePage
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                txtUserName.Text = Utils.GetCookie("DTRememberName");

                //log4net.ILog logger = log4net.LogManager.GetLogger("WebLogger");
               // logger.Debug("Test");
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string userName = txtUserName.Text.Trim();
            string userPwd = txtPassword.Text.Trim();

            if (userName.Equals("") || userPwd.Equals(""))
            {
                msgtip.InnerHtml = "请输入用户名或密码";
                return;
            }

            //Noted by King 2015/4/29 出错才记录次数
            //if (Session["AdminLoginSun"] == null)
            //{
            //    Session["AdminLoginSun"] = 1;
            //}
            //else
            //{
            //    Session["AdminLoginSun"] = Convert.ToInt32(Session["AdminLoginSun"]) + 1;
            //}

            //判断登录错误次数
            if (Session["AdminLoginSun"] != null && Convert.ToInt32(Session["AdminLoginSun"]) > 5)
            {
                msgtip.InnerHtml = "错误超过5次，关闭浏览器重新登录！";
                return;
            }
            var model =GetLoginModel(userName, userPwd, true);
            if (model == null)
            {
                if (Session["AdminLoginSun"] == null)
                {
                    Session["AdminLoginSun"] = 1;
                }
                else
                {
                    Session["AdminLoginSun"] = Convert.ToInt32(Session["AdminLoginSun"]) + 1;
                }

                msgtip.InnerHtml = "用户名或密码有误，请重试！";
                return;
            }
            Session[DTKeys.SESSION_ADMIN_INFO] = model;
            Session["AdminLoginSun"] = 1;
            Session.Timeout = 45;

            ////获取所在组织级别
            //var depart = new BLL.OrganizationBLL().GetModel(model.org_id);
            //model.departLayer = depart.class_layer;

            //写入登录日志
            //Model.siteconfig siteConfig = new BLL.siteconfig().loadConfig();
            //if (siteConfig.logstatus > 0)
            //{
                AddAdminLog("Login", "用户登录");
            //}
            //写入Cookies
            Utils.WriteCookie("DTRememberName", model.user_name, 14400);
            Utils.WriteCookie("AdminName", "Budget", model.user_name);
            Utils.WriteCookie("AdminPwd", "Budget", model.password);
            Response.Redirect("index.aspx", false);

            return;
        }

    }
}